/**
 *
 * Copyright 2012 TeleNav, Inc. All rights reserved.
 * Route.java
 *
 */
package com.telenav.snowsweeper.data;

import java.util.ListIterator;
import java.util.Vector;


/**
 *@author hhzhu
 *@date 2012-12-7
 */
public class Route
{
    Vector<Point> tracer = new Vector<Point>();

    public void addPoint(Point point) {
    	if (tracer.size() > 0 && tracer.get(tracer.size() - 1) == point)
    		return;
		tracer.add(point);
    }
    
	public int getPointSize() {
		return tracer.size();
	}
    
    public String getStringHead() {
    	return "\"route\" : [";
    }
    
    public String getStringEnd() {
    	return "]"; 
    }
    
    public void appendIndex(Point index, StringBuilder builder) {
    	builder.append("{\"id\": \"");
    	builder.append(index);
    	builder.append("\"}");
    }
    
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append(getStringHead());
        ListIterator<Point> iterator = tracer.listIterator();
        if (iterator.hasNext())
        	appendIndex(iterator.next(), builder);
        while (iterator.hasNext()) {
        	builder.append(',');
        	appendIndex(iterator.next(), builder);
        }
        builder.append(getStringEnd());
        return builder.toString();
    }
    
	public String toRequestParamers(EdgeMap edgeMap) {
		StringBuilder builder = new StringBuilder();
		ListIterator<Point> iterator = tracer.listIterator();

		Point head = null, tail;
		Edge edge;
		if (iterator.hasNext()) {
			head = iterator.next();
		}
		while (iterator.hasNext()) {
			tail = iterator.next();
			edge = edgeMap.getEdgeByPoints(head, tail);
			if (edge != null) {
				builder.append(edge.getIndex());
				builder.append('|');
			}
			head = tail;
		}
		if (builder.length() > 0)
			builder.deleteCharAt(builder.length() - 1);
		return builder.toString();
	}
    
    public Vector<Point> getPoints() {
    	return tracer;
    }
    
    public void clear() {
    	tracer.clear();
    }
}
